//
//  BaseWebViewController.swift
//  cxd4iphone
//
//  Created by hexy on 15/11/19.
//  Copyright © 2015年 hexy. All rights reserved.
//

import UIKit
import NJKWebViewProgress

class WebViewController: UIViewController {

    convenience init(title: String, URLStr: String) {
      
        self.init()
        
        let url = NSURL(string: URLStr)
        xx_print(url)
        if let url = NSURL(string: URLStr) {
            
            request = NSURLRequest(URL: url)
        }
        self.title = title
    }
    
    convenience init(title: String, htmlStr: String) {
        
        self.init()
        
        self.htmlStr = htmlStr
        self.title = title
    }
    
    lazy var htmlStr = String?()
    lazy var request = NSURLRequest?()
    
    lazy var webView: UIWebView = {
        
        let web = UIWebView(bgColor: UIColor.whiteColor())
        web.delegate = self
        return web
    }()
    
    var progressView: NJKWebViewProgressView?
    var progressProxy: NJKWebViewProgress = NJKWebViewProgress()

    
    override func loadView() {
        super.loadView()
        view = webView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.whiteColor()
        
        
        self.webView.delegate = progressProxy
        self.progressProxy.webViewProxyDelegate = self
        self.progressProxy.progressDelegate = self
        
        let progressBarHeight: CGFloat = 2.0
        let navigationBarBounds: CGRect  = self.navigationController!.navigationBar.bounds;
        let barFrame: CGRect = CGRectMake(0, navigationBarBounds.size.height - progressBarHeight, navigationBarBounds.size.width, progressBarHeight)
        self.progressView = NJKWebViewProgressView(frame: barFrame)
        self.progressView!.progressBarView.backgroundColor = UIColor.redColor()
        self.progressView?.setProgress(0, animated: true)

        

        guard let html = htmlStr else {
            
            xx_print(request)
            guard let req = request else { return }
            
            webView.loadRequest(req)
            
            return
        }
        
        webView.loadHTMLString(html, baseURL: nil)
        
    }

    
}

extension WebViewController: UIWebViewDelegate, NJKWebViewProgressDelegate {
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
      
        xx_print(request.URL?.absoluteString)
        if let requestURL = request.URL?.absoluteString {
            
            if requestURL == YPRequestViewModel.shared.callBackURL {
                
//                var arr = [UIViewController]()
//                arr = (self.navigationController?.childViewControllers.reverse())!
//                self.navigationController?.popToViewController(arr[2], animated: true)
//                
//                return false
            }
            
        }
        return true
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.addSubview(self.progressView!)
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        
        self.progressView?.removeFromSuperview()
    }
    
    func webViewProgress(webViewProgress: NJKWebViewProgress!, updateProgress progress: Float) {
        self.progressView!.setProgress(progress, animated: true)
    }
    

//    func webViewDidStartLoad(webView: UIWebView) {
//        
//        xx_showHUD("加载中...")
//    }
//    
//    func webViewDidFinishLoad(webView: UIWebView) {
//        
//        xx_hideHUD()
//    }
//    
//    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
//        
//        
//    }
}